<template>
  <div>
    <el-dialog title="合并采购订单" :visible.sync="mergeFormVisible" :close-on-click-modal="false">
      <div class="filter-container">
        <el-button-group style="float: right">
          <el-button type="primary" class="filter-item" @click="btnMergeView">生成合同</el-button>
        </el-button-group>
      </div>
      <el-table
        ref="mergeTable"
        :data="needMergeIndent"
        border
        style="width: 100%"
        @selection-change="mergeSelectionChange"
      >
        <el-table-column type="selection" align="center" />
        <el-table-column prop="orderCode" label="审批单编号" align="center" width="100px" />
        <el-table-column prop="supplierName" label="供应商" align="center" width="150px" />
        <el-table-column prop="indentCode" label="采购订单编号" align="center" width="150px" />
        <el-table-column prop="indentComment" label="备注" align="center" />
      </el-table>
    </el-dialog>
    <el-dialog title="生成合同" :visible.sync="genContractFormVisible">
      <el-form ref="genContractForm" :rules="rules" :model="mergeContractTemp" label-position="right" label-width="110px">
        <el-form-item label="采购订单编码">
          <el-input v-model="mergeContractTemp.indentCodes" :disabled="true" />
        </el-form-item>
        <el-form-item label="合同编码" prop="contractCode">
          <el-input v-model="mergeContractTemp.contractCode" />
        </el-form-item>
        <el-form-item label="合同备注" prop="contractComment">
          <el-input v-model="mergeContractTemp.contractComment" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-close" @click="genContractFormVisible = false">取消</el-button>
        <el-button icon="el-icon-check" type="primary" @click="mergeIndent()">确定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { postAction, getAction } from '@/api/manage'
import { Message } from 'element-ui'

export default {
  name: '',
  components: {
  },
  props: {
  },
  data() {
    return {
      orderId: undefined,
      supplierId: undefined,
      hideWhenEmpty: false,
      mergeContractTemp: {
        indentIds: '',
        indentCodes: '',
        contractCode: '',
        contractComment: ''
      },
      needMergeIndent: [],
      mergeRecords: [],
      mergeFormVisible: false,
      genContractFormVisible: false,
      rules: {
        contractCode: [{ required: true, message: '该项不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
  },
  methods: {
    show(orderId, supplierId, hideWhenEmpty) {
      this.mergeFormVisible = true
      this.orderId = orderId
      this.supplierId = supplierId
      this.hideWhenEmpty = hideWhenEmpty
      this.findGenIndent(orderId, supplierId)
    },
    btnMergeView() {
      this.resetTemp()
      const ids = this.mergeRecords.map(record => {
        return record.id
      })
      const codes = this.mergeRecords.map(record => {
        return record.indentCode
      })
      if (ids.length === 0) {
        Message.error('请选择要合并的记录')
        return
      }
      this.mergeContractTemp.indentIds = ids.toString()
      this.mergeContractTemp.indentCodes = codes.toString()
      this.genContractFormVisible = true
    },
    findGenIndent(oId, sId) {
      getAction('/purchase/contract/findGenIndentBy', { orderId: oId, supplierId: sId }).then(res => {
        const { data } = res
        this.needMergeIndent = data
        if (this.hideWhenEmpty && this.needMergeIndent.length === 0) {
          this.mergeFormVisible = false
          this.$emit('handleChild', { type: 'indentMerged' })
        }
      })
    },
    mergeSelectionChange(selectedRecords) {
      this.mergeRecords = selectedRecords
    },
    mergeIndent() {
      this.$refs['genContractForm'].validate((valid) => {
        if (valid) {
          postAction('/purchase/contract/merge', this.mergeContractTemp).then(({ msg }) => {
            this.genContractFormVisible = false
            this.resetTemp()
            Message.success(msg)
            this.findGenIndent(this.orderId, this.supplierId)
          })
        }
      })
    },
    resetTemp() {
      this.mergeContractTemp = {
        indentIds: '',
        indentCodes: '',
        contractCode: '',
        contractComment: ''
      }
    }

  }
}
</script>
